
package com.smc.sys.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import com.google.common.collect.Lists;
import com.smc.common.config.Global;
import com.smc.common.utils.JsonUtils;
import com.smc.common.utils.SpringContextHolder;

import com.smc.sys.model.Menu;
import com.smc.sys.model.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 菜单管理Utils
 * 
 * @author zouqh
 * @version 2015-09-08
 */
public class MenuUtils {

    private static Logger logger = LoggerFactory.getLogger(MenuUtils.class);
    
  
    private static String xmlPath = "menu.xml";

    

    public static List<Menu> getMenuList() {

        return getMenuListByConfig();
    }

    @SuppressWarnings("static-access")
    public static List<Menu> getAllMenuListByConfig() {
        String fileName = "menu.json";
        BufferedReader br = null;
        String menuStr = null;
        try {
            ClassPathResource resource = new ClassPathResource(fileName);
            InputStream inputStream = resource.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer message=new StringBuffer();
            String line = null; 
            while((line = br.readLine()) != null) { 
            message.append(line); 
            }
            menuStr = message.toString();
        } catch (Exception e) {
            logger.error("getAllMenuListByConfig 获取菜单异常 ",e);
        }
        List<Menu> list = JsonUtils.toList(menuStr, Menu.class);
        return list;
    }

    @SuppressWarnings("static-access")
    public static List<Menu> getMenuListByConfig() {
        List<Menu> menus = Lists.newArrayList();
        String menuStr = Global.getConfig("sys.menu");
        JSONArray list = JSONArray.fromObject(menuStr);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                /*JSONObject jsonObject = (JSONObject) list.get(i);
                Menu menu = (Menu) jsonObject.toBean(jsonObject, Menu.class);*/
                JSONObject jsonObject = (JSONObject) list.get(i);
                Menu menu = (Menu) JsonUtils.toBean(jsonObject.toString(), Menu.class);
                if ("1".equals(menu.getIsShow())) {
                    // menu.setParent(new Menu(jsonObject.getString("parentId")));
                    menus.add(menu);
                }
            }
        }

        return menus;
    }

    /**
     * public static Menu getMenuByCode(String code) { List<Menu> entitys = null; Menu entity = new Menu();
     * entity.setCode(code); entitys = menuDao.findList(entity); return entitys.get(0); }
     */

    public static ValiterMessage valiter(Menu entity) {
        // TODO Auto-generated method stub
        ValiterMessage valiterMessage = new ValiterMessage();
        valiterMessage.setStatus(true);

        return valiterMessage;
    }
}